<?php
class Form_Adminlogin extends QForm
{
	function __construct($action)
	{
		// 调用父类的构造函数
		parent::__construct('form_adminlogin', $action);

		// 从配置文件载入表单
		$filename = rtrim(dirname(__FILE__), '/\\') . DS . 'adminlogin_form.yaml';
		$this->loadFromConfig(Helper_YAML::loadCached($filename));
		$this->addValidations(Manager::meta());
		$this['check_pass']->addValidations(array($this,'check_code'),'验证码错误!');
		$this['user_name']->addValidations(array($this,'check_user_name'),'账户不存在或被锁定不能登陆!');
		$this['user_pass']->addValidations(array($this,'check_user_pass'),'您的账户或密码错误!');
	}
	/**
	 * 
	 * 验证码验证
	 */
	function check_code()
	{
		return Helper_Ckcode::check($this['check_pass']->value);
	}
	/**
	 * 
	 * 登陆账户确认
	 */
	function check_user_name()
	{
		$user_name = $this['user_name']->value;
		$user_obj = Manager::find('user_name = ? and `status`',$user_name)->getOne();
		if ($user_obj->id()) {
			return true;
		}
		return false;
	}
	/**
	 * 
	 * 登陆账户密码
	 */
	function check_user_pass()
	{
		$user_name = trim($this['user_name']->value);
		$user_pass = trim($this['user_pass']->value);
		if ($user_pass == '') return false;
		$user_obj = Manager::find('user_name = ? and user_pass = ? and `status`',$user_name,Helper_Common::system_md5($user_pass))->getOne();
		if ($user_obj->id()) {
			// 账户密码正确
			$_SESSION['ADMIN_USER'] = $user_obj->toArray();
			$_SESSION['ADMIN_USER']['power'] = $user_obj->user_power?($user_obj->user_power):($user_obj->group->power);
			return true;
		}
		return false;
	}
}